<template>
	<view>
		<page-head :title="title"></page-head>
		<view class="uni-common-mt uni-padding-wrap " v-if="!wx">
			<view class="uni-form-item uni-column">
				<view class="title">用户名</view>
				<input v-model="user.username" class="uni-input" focus placeholder="请输入用户名" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">密码</view>
				<input v-model="user.password" class="uni-input" password type="text" placeholder="这是一个密码输入框" />
			</view>	
			
			<button type="primary" style="margin-top: 30rpx;" @click="toLogin">登录</button>
			<button type="primary" style="margin-top: 30rpx;">第三方登录</button>
		</view>
		
		<view class="uni-common-mt uni-padding-wrap " v-if="wx">
		
			<button type="primary" style="margin-top: 30rpx;" @click="toLoginWx">欢迎使用</button>
		</view>
		
		<!-- <view class="uni-padding-wrap uni-common-mt">
			<view class="uni-btn-v">
				<button @tap="navigateTo">跳转新页面，并传递数据</button>
				<button @tap="navigateBack">返回上一页</button>
				<button @tap="redirectTo">在当前页面打开</button>
				<button @tap="switchTab">切换到模板选项卡</button>
				<button v-if="!hasLeftWin" @tap="reLaunch">关闭所有页面，打开首页</button>
				<!-- #ifdef APP-PLUS -->
				<button @tap="customAnimation">使用自定义动画打开页面</button>
				<!-- #endif -->
				<!-- #ifdef APP-PLUS || H5 -->
				<button @tap="preloadPage">预载复杂页面</button>
				<!-- #endif -->
				<!-- #ifdef APP-PLUS -->
				<button @tap="unPreloadPage">取消页面预载</button>
				<!-- #endif -->
				<!-- #ifdef APP-PLUS || H5 -->
				<button @tap="navigateToPreloadPage">打开复杂页面</button>
				<!-- #endif -->
			</view>
		</view> -->
	</view>
</template>
<script>
	const preloadPageUrl = '/pages/extUI/calendar/calendar'
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				wx: false,
				title: '系统登录',
				user:{
					username: '',
					password: '',
					openid: '',
				}
			}
		},
		computed: {
			...mapState({
				hasLeftWin: state => !state.noMatchLeftWindow
			})
		},
		created() {
			console.log("登录的路由",this.$route.fullPath, window.location.href)
			// window.addEventListener('storage', this.afterQRScan)
			const url = window.location.href;
			
			// 使用 URLSearchParams 获取查询参数
			const urlParams = new URLSearchParams(window.location.search);
			
			// 判断 'openid' 是否存在
			const openid = urlParams.get('openid');
			
			if (openid) {
			  this.wx = true
			  this.user.openid = openid;
			  console.log("openid 存在，值为: ", openid);
			  console.log("wx",this.wx)
			} else {
			  console.log("openid 不存在");
			}
		},
		methods: {
			
			toLogin(){
				console.log(this.user)
				uni.request({
				    url: this.$config.apiUrl+"login",
				    dataType: 'json',
				    method: 'POST',
				    header: {
				    	"content-type": "application/json"
				    },
				    data: this.user,
				    success: (res) => {
				        console.log(res.data);
				        this.text = 'request success';
						if(res.data.message === '登录成功'){
							uni.setStorageSync("jwt",res.data.token)
							uni.setStorageSync("tokenInfo",res.data.tokenInfo)
							uni.setStorageSync("userid",res.data.tokenInfo.userid)
							uni.reLaunch({
								url: '/pages/tabBar/core/core',//跳转到主页
							});
						}
				    }
				});
			},
			toLoginWx(){
				console.log(this.user)
				uni.request({
				    url: this.$config.apiUrl+"wx/login",
				    dataType: 'json',
				    method: 'POST',
				    header: {
				    	"content-type": "application/json"
				    },
				    data: this.user,
				    success: (res) => {
				        console.log(res.data);
				        this.text = 'request success';
						if(res.data.message === '登录成功'){
							uni.setStorageSync("jwt",res.data.token)
							uni.setStorageSync("tokenInfo",res.data.tokenInfo)
							uni.setStorageSync("userid",res.data.tokenInfo.userid)
							uni.reLaunch({
								url: '/pages/tabBar/core/core',//跳转到主页
							});
						}
				    }
				});
			},
			//跳转新页面，并传递数据
			navigateTo() {
				uni.navigateTo({
					url: '/'
				})
			},
			//返回上一页
			navigateBack() {
				uni.navigateBack();
			},
			// 在当前页面打开
			redirectTo() {
				uni.redirectTo({
					url: '/pages/API/navigator/new-page/new-vue-page-1'
				});
			},
			//切换到模板选项卡
			switchTab() {
				uni.switchTab({
					url: '/pages/tabBar/template/template'
				});
			},
			//关闭所有页面，打开首页
			reLaunch() {
				if (this.hasLeftWin) {
					uni.reLaunch({
						url: '/pages/component/view/view'
					});
					return;
				}
				uni.reLaunch({
					url: '/pages/tabBar/component/component'
				});
			},
			//使用自定义动画打开页面
			customAnimation(){
				uni.navigateTo({
					url: 'new-page/new-vue-page-1?data=使用自定义动画打开页面',
					animationType: 'slide-in-bottom',
					animationDuration: 200
				})
			},
			//预载复杂页面 
			preloadPage(){
				uni.preloadPage({
					url: preloadPageUrl,
					success(){
						uni.showToast({
							title:'页面预载成功'
						})
					},
					fail(e){
						console.error(e);
						uni.showToast({
							title:'页面预载失败'
						})
					}
				})
			},
			//取消页面预载
			unPreloadPage(){
				uni.unPreloadPage({
					url: preloadPageUrl
				})
			},
			//打开复杂页面
			navigateToPreloadPage(){
				uni.navigateTo({
					url: preloadPageUrl
				})
			}
		}
	}
</script>
